/*
 * PHP To JavaScript Functions
 * Version: 0.1 Beta
 * Editor: Bilal El Moussaoui {W3arabia}
 */

function str_replace(search, replace, subject) {
  var Str = new String(subject);
	return Str.replace(search, replace);
}

function substr($string, start, length) {
	var Str = new String($string);
	return Str.substr(start, length);
}

function strlen($string) {
	var Str = new String($string);
	return Str.length;
}

function strtoupper($string) {
	var Str = new String($string);
	return Str.toUpperCase();
}

function strtolower($string) {
	var Str = new String($string);
	return Str.toLowerCase();
}

function ucfirst($string) {
	var Str = strtoupper(substr($string, 0, 1)) + substr($string, 1, strlen($string));
	return Str;
}

function lcfirst($string) {
	var Str = strtolower(substr($string, 0, 1)) + substr($string, 1, strlen($string));
	return Str;
}

function gettype($var) {
	if ($var === undefined) {
		return 'undefined';
	}
	if ($var === null) {
		return 'null';
	}
	return strtolower(Object.prototype.toString.call($var).split(' ').pop().split(']').shift());
}

function addslashes(str) {
	return(str + '').replace(/[\\"']/g, '\\$&').replace(/\u0000/g, '\\0');
}

function stripslashes(str) {
	return(str + '').replace(/\\(.?)/g, function (s, n1) {
			switch (n1) {
			case '\\': 
				return '\\';
			case '0': 
				return '\u0000';
			case '': 
				return '';
			default: 
				return n1;
			}
		});
}

function explode(delimiter,str){
	return str.split(delimiter);
}
		
function implode(glue ,pieces ){
	return pieces.join(glue);
}

function print_r(expression){
    if(expression.constructor == Array || expression.constructor == Object){
        document.write("<ul>");
        for(var p in expression){
            if(expression[p].constructor == Array|| expression[p].constructor == Object){
                document.write("<li>["+p+"] => "+typeof(expression)+"</li>");
                document.write("<ul>");
                print_r(expression[p]);
                document.write("</ul>");
            } else {
                document.write("<li>["+p+"] => "+expression[p]+"</li>");
            }
        }
        document.write("</ul>");
    }
} 

function setcookie(name, value, expire , path)
{
	var expires = '';
	if (expire  != undefined && expire  != 0)
	{
		var date = new Date();
		if (expire  < 0)
		{
			date.setTime(0);
		}
		else
		{
			date.setTime(date.getTime() + Math.ceil(expire  * 86400 * 1000));
		}
		expires = '; expires=' + date.toGMTString();
	}
	path = path || '/';
	document.cookie = name + '=' + encodeURIComponent(value) + expires + '; path=' + path;
}

function chr(ascii){
	return String.fromCharCode(ascii);
}

function ord(str){
	return String.charCodeAt(str);
}

function urlencode(str){
	return encodeURIComponent(str);
}

function urldecode(str){
	return decodeURIComponent(str);
}

function sqrt(arg){
	return Math.sqrt(arg);
}

function abs(number ){
	return Math.abs(number );
}

function acos(arg){
	return Math.acos(arg);
}

function asin (arg){
	return Math.asin(arg);
}

function atan(arg){
	return Math.atan(arg);
}

function atan2(y,x){
	return Math.atan2(y,x);
}

function ceil(value){
	return Math.ceil(value);
}

function cos(arg){
	return Math.cos(arg);
}

function exp(arg){
	return Math.exp(arg);
}

function floor(value){
	return Math.floor(value);
}
function log(arg){
	return Math.log(arg);
}

function tan(arg){
	return Math.tan(arg);
}

function sin(arg){
	return Math.sin(arg);
}

function round(arg){
	return Math.round(arg);
}

function pow(base ,exp){
	return Math.pow(base,exp);
}

function is_numeric($var) {
  return !isNaN(parseFloat($var)) && isFinite($var);
}

function trim(str) {
	return str.replace(/(^\s*)|(\s*$)/g,"")
}

function ltrim(str){
	return str.replace(/^\s+/,"");
}

function rtrim(str){
	return str.replace(/\s+$/,"");
}

function rad2deg(arg){
	return ((arg / Math.PI) * 180);
}

function pi(){
	return Math.PI;
}

function rand(min,max){
	var args = arguments.length;
	if(args === 0){
		min = 0;
		max = 2147483647;
	}else if(args === 1){
		throw new Error('Warning : rand() exepets to parametrs , 1 given');
	}
	return Math.floor(Math.random() * (max - min + 1)) + min;
}

function mt_rand(min,max){
	var args = arguments.length;
	if(args === 0){
		min = 0;
		max = 2147483647;
	}else if(args === 1){
		throw new Error('Warning : rand() exepets to parametrs , 1 given');
	}
	return Math.floor(Math.random() * (max - min + 1)) + min;
}


function join(glue ,pieces ){
	return implode(glue,pieces);
}

function function_exists(function_name) {
	if (typeof function_name == 'string'){
		return (typeof this.window[function_name] == 'function');
	}else{
		return (function_name instanceof Function);
	}
}

function lcfirst($string){
    var Str = strtolower(substr($string,0,1)) + substr($string,1,strlen($string));
    return Str;
} 

function addslashes(str){
    return (str + '').replace(/[\\"']/g, '\\$&').replace(/\u0000/g, '\\0');
}
function stripslashes(str){
    return (str+'').replace(/\\(.?)/g, function (s, n1){
        switch (n1){
            case '\\':
                return '\\';
            case '0':
                return '\u0000';
            case '':
                return '';
            default:
                return n1;
        }
    });
} 

function explode(delimiter,str){
    return str.split(delimiter);
}
        
function implode(glue ,pieces ){
    return pieces.join(glue);
}

function print_r(expression){
    if(expression.constructor == Array || expression.constructor == Object){
        document.write("<ul>");
        for(var p in theObj){
            if(expression[p].constructor == Array|| expression[p].constructor == Object){
                document.write("<li>["+p+"] => "+typeof(expression)+"</li>");
                document.write("<ul>");
                print_r(expression[p]);
                document.write("</ul>");
            } else {
                document.write("<li>["+p+"] => "+expression[p]+"</li>");
            }
        }
        document.write("</ul>");
    }
} 

function setcookie(name, value, expire , path)
{
    var expires = '';
    if (expire  != undefined && expire  != 0)
    {
        var date = new Date();
        if (expire  < 0)
        {
            date.setTime(0);
        }
        else
        {
            date.setTime(date.getTime() + Math.ceil(expire  * 86400 * 1000));
        }
        expires = '; expires=' + date.toGMTString();
    }
    path = path || '/';
    document.cookie = name + '=' + encodeURIComponent(value) + expires + '; path=' + path;
}

function chr(ascii){
    return String.fromCharCode(ascii);
}

function ord(str){
    return String.charCodeAt(str);
} 

function urlencode(str){
    return encodeURIComponent(str);
}

function urldecode(str){
    return decodeURIComponent(str);
}

function sqrt(arg){
    return Math.sqrt(arg);
}

function abs(number ){
    return Math.abs(number );
}

function acos(arg){
    return Math.acos(arg);
}

function asin (arg){
    return Math.asin(arg);
}

function atan(arg){
    return Math.atan(arg);
}

function atan2(y,x){
    return Math.atan2(y,x);
}

function ceil(value){
    return Math.ceil(value);
}

function cos(arg){
    return Math.cos(arg);
}

function exp(arg){
    return Math.exp(arg);
}

function floor(value){
    return Math.floor(value);
}

function log(arg){
    return Math.log(arg);
}

function tan(arg){
    return Math.tan(arg);
}

function sin(arg){
    return Math.sin(arg);
}

function round(arg){
    return Math.round(arg);
}

function pow(base ,exp){
    return Math.pow(base,exp);
}

function is_numeric($var) {
  return !isNaN(parseFloat($var)) && isFinite($var);
} 

function trim(str) {
    return str.replace(/(^\s*)|(\s*$)/g,"")
}

function ltrim(str){
    return str.replace(/^\s+/,"");
}

function rtrim(str){
    return str.replace(/\s+$/,"");
}

function function_exists(function_name) {
    if (typeof function_name == 'string'){
        return (typeof this.window[function_name] == 'function');
    }else{
        return (function_name instanceof Function);
    }
}

